package com.hxht.cmp.controller;

import com.github.pagehelper.Page;
import com.hxht.cmp.common.Constants;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.Favorite;
import com.hxht.cmp.entity.User;
import com.hxht.cmp.pojo.CourtroomEx;
import com.hxht.cmp.pojo.FavoritePojo;
import com.hxht.cmp.pojo.FavoriteResult;
import com.hxht.cmp.service.FavoriteService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author alice on 2017/8/16 0016.
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Resource
    private FavoriteService favoriteService;

    @RequestMapping("/like")
    public ResultResponse insertFavorite(String planUid) {
        if (ObjectUtils.isEmpty(planUid) || ObjectUtils.isEmpty(planUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        Favorite favorite = new Favorite();
        favorite.setPlanUid(planUid);
        favorite.setUserUid(user.getUid());
        favorite = favoriteService.selectFavorite(favorite);
        if (!ObjectUtils.isEmpty(favorite)) {
            return ResultResponse.init(1050, "您已收藏!");
        }
        favorite = new Favorite();
        favorite.setPlanUid(planUid);
        favorite.setDateCreated(new Date());
        favorite.setUserUid(user.getUid());
        int success = favoriteService.insertFavorite(favorite);
        return success > 0 ? ResultResponse.success() : ResultResponse.failed();
    }

    @RequestMapping("/unlike")
    public ResultResponse deleteFavorite(String id, String planUid) {
        if (ObjectUtils.isEmpty(id) && ObjectUtils.isEmpty(planUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        Favorite favorite = new Favorite();
        if (!ObjectUtils.isEmpty(id)) {
            if (!isNumeric(id)) {
                return ResultResponse.failed(ResultCode.PARAMETER_ERROR);
            }
            favorite.setId(Long.parseLong(id));
        }
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        favorite.setUserUid(user.getUid());
        if (!ObjectUtils.isEmpty(planUid)) {
            favorite.setPlanUid(planUid);
        }
        int success = favoriteService.deleteFavorite(favorite);
        return success > 0 ? ResultResponse.success() : ResultResponse.failed();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    @RequestMapping("/selectFavorite")
    public ResultResponse selectFavorite(FavoritePojo favoritePojo) {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        favoritePojo.setUserUid(user.getUid());
        Page<FavoriteResult> favorites = favoriteService.pageFavorite(favoritePojo);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < favorites.getResult().size(); i++) {
            FavoriteResult favoriteResult = favorites.get(i);
            favoriteResult.setStart(format.format(favoriteResult.getStartDate()));
            favorites.set(i, favoriteResult);
            if (!ObjectUtils.isEmpty(favoriteResult.getCourtRoomUid())) {
                CourtroomEx courtroomEx = Constants.COURTROOM_MAP.get(favoriteResult.getCourtRoomUid());
                favoriteResult.setCourtroomName(courtroomEx.getName());
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("count", favorites.getTotal());
        result.put("result", favorites.getResult());
        return ResultResponse.success(result);
    }


}
